Imports IICModel.ConversionObjects
Imports IICModel.BusObjects
Imports IICModel.ConversionObjects.Mappers
Imports IICModel.BusObjects.Mappers

Namespace DataConbversionLogic
    Public Class CANDIDATMemberStudentConverter
        Inherits BaseDataConverter

        Protected Overrides Sub mapData(ByVal source As org.model.lib.Model.IModelObject, _
                                        ByRef destination As org.model.lib.Model.IModelObject)

            Dim s As CANDIDAT = CType(source, CANDIDAT)
            Dim d As Student = CType(destination, Student)

            d.MemberNumber = CType(s.MemberNo, Integer?)
            d.FirstName = MEMBERSConverter.parseFirstName(Trim(s.Name1))
            d.LastName = MEMBERSConverter.parseLastName(Trim(s.Name1))

            If String.IsNullOrEmpty(d.FirstName) Then
                Me.logMsg(s.Id & " skipped, FirstName is empty ")
                destination = Nothing
                Exit Sub
            End If
            If String.IsNullOrEmpty(d.LastName) Then
                Me.logMsg(s.Id & " skipped, LastName is empty ")
                destination = Nothing
                Exit Sub
            End If

            'd.Company = Trim(s.COMPANY)
            'd.HomePhone = Trim(s.TEL)
            'd.OfficePhone = Trim(s.TEL)
            d.InformCompanyFlag = False
            'd.Address1 = Trim(s.ADDRESSONE)
            'd.Address2 = Trim(s.ADDRESSTWO)
            'd.Address3 = Trim(s.TOWN)
            'd.Address4 = Trim(s.PostCode)
            d.IsMember = True
            d.CountryId = CType(IIf(s.NATIONALITY = "C", 1, 2), Integer?)
            'd.LastPaymentDate = s.LastPayDt
            'd.LastSubscriptionYear = s.LastYrPaid
            'd.MemberSince = s.AdmDt
            'd.IsMemberActive = CType(IIf(Trim(s.STATUS) = "I", 0, 1), Integer?)
            d.DataConversionSourceTable = getTableName()
            'd.DistrictId = CType(s.DISTRICT, Integer?)

        End Sub

        Protected Overrides Function getTableName() As String
            Return "CANDIDAT"
        End Function

        Protected Overrides Sub instantiateMappers()

            Me.dataConvMapper = New CANDIDATDBMapper()
            Me.newdbMapper = New StudentDBMapper

        End Sub

        Protected Overrides Function checkData(ByVal source As IModelObject, _
                                  ByVal destination As IModelObject) As Boolean



            ' Console.WriteLine("Chkeck " & CType(source, CANDIDAT).MemberNo)
            Dim lSource As CANDIDAT = CType(source, CANDIDAT)
            If String.IsNullOrEmpty(lSource.Name1) Then
                Me.logMsg(lSource.Id & " skipped, name is blank ")
                Return False
            End If

            Dim memno As String = CStr(CInt(lSource.MemberNo))
            Dim exists As Boolean = StudentDataUtils.findOne("membernumber={0}", _
                                                memno) IsNot Nothing

            If exists Then
                Me.logMsg(lSource.Id & " skipped, member with number " & memno & " already exists ")
                Return False
            End If
            Return True

        End Function
    End Class

End Namespace